<template>
    <div class="createPost-container">
        <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">
            <div class="createPost-main-container">
                <el-form-item prop="store_id" style="margin-bottom: 40px;" label-width="90px" label="门店:">
                    <el-select v-model="postForm.store_id">
                        <el-option :label="'平台'" :value="'0'">平台</el-option>
                        <el-option v-for="(store, index) in store_list" :key="index" :label="store.name" :value="store.id">{{ store.name }}</el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="title" style="margin-bottom: 40px;" label-width="90px" label="标题:">
                    <el-input v-model="postForm.title" placeholder="请输入标题" />
                </el-form-item>
                <el-form-item prop="content" style="margin-bottom: 40px;" label-width="90px" label="正文:">
                    <tinymce ref="editor" v-model="postForm.content" :height="300" />
                </el-form-item>
                <el-form-item prop="status" style="margin-bottom: 40px;" label-width="90px" label="状态:">
                    <el-select v-model="postForm.status">
                        <el-option label="下线" value="0">下线</el-option>
                        <el-option label="上线" value="1">上线</el-option>
                    </el-select>
                </el-form-item>
                <el-button v-loading="loading" style="margin-left: 10px;" type="primary" @click="submitForm">
                    保存
                </el-button>
            </div>
        </el-form>
    </div>
</template>

<script>
import { fetch, update, create } from '@/notice/api/notice'
import { queryList as queryStoreList } from '@/shop/api/store'
import Tinymce from '@/components/Tinymce'

const defaultForm = {
    store_id: '0',
    title: '',
    content: '',
    status: '0'
}

export default {
    name: 'NoticeDetail',
    components: { Tinymce },
    props: {
        isEdit: {
            type: Boolean,
            default: false
        }
    },
    data() {
        const validateRequire = (rule, value, callback) => {
            if (value === '' || value === null || value === undefined) {
                this.$message({
                    message: rule.message,
                    type: 'error'
                })
                callback(new Error())
            } else {
                callback()
            }
        }
        return {
            id: 0,
            postForm: Object.assign({}, defaultForm),
            typeList: [],
            loading: false,
            userListOptions: [],
            rules: {
                store_id: [{ validator: validateRequire, message: '门店为必填字段', trigger: 'blur' }],
                title: [{ validator: validateRequire, message: '标题为必填字段', trigger: 'blur' }],
                content: [{ validator: validateRequire, message: '请输入公告正文', trigger: 'blur' }]
            },
            tempRoute: {},
            dialogFormGps: false,
            store_list: []
        }
    },
    created() {
        queryStoreList().then(res => {
            this.store_list = res.data.items
        })
        if (this.isEdit === true) {
            this.id = this.$route.query.id
            this.fetchData()
        }
    },
    methods: {
        fetchData() {
            fetch({ id: this.id }).then(response => {
                this.postForm.title = response.data.title
                this.postForm.content = response.data.content
                if (this.postForm.content !== undefined && this.postForm.content !== null && this.postForm.content !== '') {
                    this.$refs['editor'].setContent(this.postForm.content)
                }
                this.postForm.store_id = response.data.store_id.toString()
                this.postForm.status = response.data.status
            }).catch(err => {
                console.log(err)
            })
        },
        submitForm() {
            this.$refs.postForm.validate(valid => {
                if (valid) {
                    this.loading = true
                    if (this.isEdit === true) {
                        const data = this.postForm
                        data.id = this.id
                        update(data).then(response => {
                            this.loading = false
                            const status = response.data.status
                            if (status === '0') {
                                this.$notify({
                                    title: '成功',
                                    message: '修改成功',
                                    type: 'success',
                                    duration: 2000
                                })
                            } else if (status === '1') {
                                this.$notify({
                                    title: '失败',
                                    message: '手机号用户不存在',
                                    type: 'error',
                                    duration: 2000
                                })
                            } else {
                                this.$notify({
                                    title: '失败',
                                    message: '修改失败',
                                    type: 'error',
                                    duration: 2000
                                })
                            }
                        }).catch(err => {
                            console.log(err)
                            this.loading = false
                        })
                    } else {
                        const data = this.postForm
                        create(data).then(response => {
                            this.loading = false
                            const status = response.data.status
                            if (status === '0') {
                                this.$notify({
                                    title: '成功',
                                    message: '添加成功',
                                    type: 'success',
                                    duration: 2000
                                })
                                // this.postForm.status = 'published'
                                this.$router.push('/operate/notice')
                            } else if (status === '1') {
                                this.$notify({
                                    title: '失败',
                                    message: '手机号用户不存在',
                                    type: 'error',
                                    duration: 2000
                                })
                            } else {
                                this.$notify({
                                    title: '失败',
                                    message: '添加失败',
                                    type: 'error',
                                    duration: 2000
                                })
                            }
                        }).catch(err => {
                            console.log(err)
                            this.loading = false
                        })
                    }
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        },
        draftForm() {
            if (this.postForm.content.length === 0 || this.postForm.title.length === 0) {
                this.$message({
                    message: '请填写必要的标题和内容',
                    type: 'warning'
                })
                return
            }
            this.$message({
                message: '保存成功',
                type: 'success',
                showClose: true,
                duration: 1000
            })
            this.postForm.status = 'draft'
        }
    }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
    position: relative;

    .createPost-main-container {
        padding: 40px 45px 20px 50px;

        .postInfo-container {
            position: relative;
            @include clearfix;
            margin-bottom: 10px;

            .postInfo-container-item {
                float: left;
            }
        }
    }

    .word-counter {
        width: 40px;
        position: absolute;
        right: 10px;
        top: 0px;
    }
}

.article-textarea ::v-deep {
    textarea {
        padding-right: 40px;
        resize: none;
        border: none;
        border-radius: 0px;
        border-bottom: 1px solid #bfcbd9;
    }
}
</style>
